package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Oilrecord;
import com.woniuxy.caradmin.entity.vo.OilRecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.OilrecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
public class OilrecordController {
    @Autowired
    private OilrecordService oilrecordService;

    @GetMapping("/oilrecordList")
    @ApiOperation("查询所有的加油记录")
    public DataGridView selectAllOilRecords(OilRecordVo oilRecordVo) {
        Page<Object> page = PageHelper.startPage(oilRecordVo.getPage(), oilRecordVo.getLimit());
        List<Oilrecord> oilrecords = oilrecordService.selectAllOilRecords(oilRecordVo);
        if(oilrecords!=null && oilrecords.size()>0){
            return  new DataGridView(page.getTotal(),oilrecords);
        } else {
            throw new CarException(ResultCode.OILRECORDISNULL, Constaint.OILRECORDISNULL);
        }

    }

    @GetMapping("/addOilRecord")
    @ApiOperation("增加加油记录")
    public R addOilRecord(Oilrecord oilrecord) {
        int addOilRecord = oilrecordService.addOilRecord(oilrecord);
        if (addOilRecord != 0) {
            return R.ok().code(ResultCode.OILRECORDADDISSUC).message(Constaint.OILRECORDADDISSUC);
        } else {
            throw new CarException(ResultCode.OILRECORDADDISFAI, Constaint.OILRECORDADDISFAI);
        }
    }

    @GetMapping("/updateOilRecord")
    @ApiOperation("修改加油记录")
    public R updateOilRecord(Oilrecord oilrecord) {
        int updateOilRecord = oilrecordService.updateOilRecord(oilrecord);
        if (updateOilRecord != 0) {
            return R.ok().code(ResultCode.OILRECORDUPDATEISSUC).message(Constaint.OILRECORDUPDATEISSUC);
        } else {
            throw new CarException(ResultCode.OILRECORDUPDATEISFAI, Constaint.OILRECORDUPDATEISFAI);
        }
    }

    @GetMapping("/deleteOilRecord")
    @ApiOperation("删除加油记录")
    public R deleteOilRecord(Integer id) {
        int deleteOilRecord = oilrecordService.deleteOilRecord(id);
        if (deleteOilRecord != 0) {
            return R.ok().code(ResultCode.OILRECORDDELETEISSUC).message(Constaint.OILRECORDDELETEISSUC);
        } else {
            throw new CarException(ResultCode.OILRECORDDELETEISFAI, Constaint.OILRECORDDELETEISFAI);
        }

    }

    @GetMapping("/goOilRecord")
    public ModelAndView goLogin(ModelAndView modelAndView){
        modelAndView.setViewName("oilrecord");
        return modelAndView;
    }


}

